package com.wisdom.admin.service.impl;

import com.wisdom.admin.dto.UmsMenuNode;
import com.wisdom.admin.dto.UmsMenuParams;
import com.wisdom.admin.mbg.mapper.UmsAdminMenuMapper;
import com.wisdom.admin.mbg.model.UmsAdminMenu;
import com.wisdom.admin.mbg.model.UmsAdminMenuExample;
import com.wisdom.admin.service.UmsAdminMenuService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author ming
 * @ClassName UmsAdminMenuServiceImpl.java
 * @Description 后台管理菜单服务层接口实现类
 * @createTime 2021年04月11日 17:17:00
 */
@Service
public class UmsAdminMenuServiceImpl implements UmsAdminMenuService {
    private static final Logger LOGGER = LoggerFactory.getLogger(UmsAdminMenuServiceImpl.class);

    @Autowired(required = false)
    private UmsAdminMenuMapper umsAdminMenuMapper;

    @Override
    public List<UmsMenuNode> getLists() {
        UmsAdminMenuExample example = new UmsAdminMenuExample();
        example.createCriteria().andStatusEqualTo(1);
        List<UmsAdminMenu> lists = umsAdminMenuMapper.selectByExample(example);
        List<UmsMenuNode> result = lists.stream()
                .filter(menu -> menu.getPid().equals(0L))
                .map(menu -> covertMenuNode(menu,lists)).collect(Collectors.toList());
        return result;
    }

    /**
     * 将UmsAdminMenu转化为UmsMenuNode并设置children属性
     */
    private UmsMenuNode covertMenuNode(UmsAdminMenu menu,List<UmsAdminMenu> menuList){
        UmsMenuNode menuNode = new UmsMenuNode();
        BeanUtils.copyProperties(menu,menuNode);
        List<UmsMenuNode> children = menuList.stream()
                .filter(subMenu -> subMenu.getPid().equals(menu.getId()))
                .map(subMenu -> covertMenuNode(subMenu,menuList)).collect(Collectors.toList());
        menuNode.setChildren(children);
        return menuNode;
    }
}
